package com.ztom.v2;

import java.util.Arrays;

/**
 * @author ZhangTao
 */
public class Code08LongestSubstringWithoutRepeatingCharacters {

    public int lengthOfLongestSubstring(String s) {
        if (s.isEmpty()) {
            return 0;
        }
        if (s.length() == 1) {
            return 1;
        }

        int max = 0;
        char[] charArray = s.toCharArray();
        int[] index = new int[128];
        Arrays.fill(index, -1);
        int l = 0;
        for (int r = 0; r < charArray.length; r++) {
            l = Math.max(l, index[charArray[r]] + 1);
            max = Math.max(max, r - l + 1);
            index[charArray[r]] = r;
        }
        return max;
    }
}
